package com.gccloud.dataroom.core.module.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.common.exception.GlobalException;
import com.gccloud.common.utils.BeanConvertUtils;
import com.gccloud.common.vo.PageVO;
import com.gccloud.dataroom.core.module.device.dao.DataRoomDeviceDao;
import com.gccloud.dataroom.core.module.device.dto.DataRoomDeviceDTO;
import com.gccloud.dataroom.core.module.device.dto.DeviceSearchDTO;
import com.gccloud.dataroom.core.module.device.entity.DataRoomDeviceEntity;
import com.gccloud.dataroom.core.module.device.service.IDataRoomDeviceService;
import com.gccloud.dataroom.core.module.device.vo.DataRoomDeviceVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author hongyang
 * @version 1.0
 * @date 2023/3/13 11:05
 */
@Service
@Slf4j
public class DataRoomDeviceServiceImpl extends ServiceImpl<DataRoomDeviceDao, DataRoomDeviceEntity> implements IDataRoomDeviceService {

    public static final int YES = 1;
    public static final int NO = 0;

    @Override
    public PageVO<DataRoomDeviceEntity> getPage(DeviceSearchDTO searchDTO) {
        LambdaQueryWrapper<DataRoomDeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(searchDTO.getDeviceName()), DataRoomDeviceEntity::getDeviceName, searchDTO.getDeviceName());
        queryWrapper.eq(StringUtils.isNotBlank(searchDTO.getId()), DataRoomDeviceEntity::getId, searchDTO.getId());
        queryWrapper.orderByAsc(DataRoomDeviceEntity::getCreateDate);
        PageVO<DataRoomDeviceEntity> page = this.page(searchDTO, queryWrapper);

        return page;
    }

    @Override
    public List<DataRoomDeviceVO> getList(DataRoomDeviceDTO searchDTO) {
        String id = searchDTO.getId();
        String deviceName = searchDTO.getDeviceName();
        String deviceIp = searchDTO.getDeviceIp();
        String devicePort = searchDTO.getDevicePort();

        LambdaQueryWrapper<DataRoomDeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(id), DataRoomDeviceEntity::getId, id);
        queryWrapper.eq(StringUtils.isNotEmpty(deviceName), DataRoomDeviceEntity::getDeviceName, deviceName);
        queryWrapper.eq(StringUtils.isNotEmpty(deviceIp), DataRoomDeviceEntity::getDeviceIp, deviceIp);
        queryWrapper.eq(StringUtils.isNotEmpty(devicePort), DataRoomDeviceEntity::getDevicePort, devicePort);

        queryWrapper.orderByDesc(DataRoomDeviceEntity::getCreateDate);
        List<DataRoomDeviceEntity> list = this.list(queryWrapper);
        System.out.println(list);


        List<DataRoomDeviceVO> voList = Lists.newArrayList();
        for (DataRoomDeviceEntity entity : list) {
            DataRoomDeviceVO mapVO = BeanConvertUtils.convert(entity, DataRoomDeviceVO.class);
            voList.add(mapVO);
        }
        return voList;
    }




    @Override
    public String add(DataRoomDeviceDTO mapDTO) {

        DataRoomDeviceEntity mapEntity = BeanConvertUtils.convert(mapDTO, DataRoomDeviceEntity.class);

        this.save(mapEntity);

        return mapEntity.getId();
    }

    @Override
    public void update(DataRoomDeviceDTO roomDeviceDTO) {
        if (StringUtils.isBlank(roomDeviceDTO.getId())) {
            throw new GlobalException("设备id不能为空");
        }
        DataRoomDeviceEntity old = this.getById(roomDeviceDTO.getId());
        if (old == null) {
            throw new GlobalException("设备不存在");
        }
        LambdaUpdateWrapper<DataRoomDeviceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DataRoomDeviceEntity::getId, roomDeviceDTO.getId());
        // 修改名称
        updateWrapper.set(DataRoomDeviceEntity::getDeviceName, roomDeviceDTO.getDeviceName());
        updateWrapper.set(DataRoomDeviceEntity::getDeviceIp, roomDeviceDTO.getDeviceIp());
        updateWrapper.set(DataRoomDeviceEntity::getDevicePort, roomDeviceDTO.getDevicePort());
        updateWrapper.set(DataRoomDeviceEntity::getDeviceLabel, roomDeviceDTO.getDeviceLabel());
        updateWrapper.set(DataRoomDeviceEntity::getDeviceCatetory, roomDeviceDTO.getDeviceCatatory());


        this.update(updateWrapper);
    }


    @Override
    public boolean delete(String id) {
        if (StringUtils.isBlank(id)) {
            return true;
        }
        DataRoomDeviceEntity deviceEntity = this.getById(id);
        if (deviceEntity == null) {
            return true;
        }
        LambdaQueryWrapper<DataRoomDeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataRoomDeviceEntity::getId, deviceEntity.getId());
        List<DataRoomDeviceEntity> list = this.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return false;
        }
        this.removeById(id);
        return true;
    }


    @Override
    public DataRoomDeviceEntity info(String id) {
        DataRoomDeviceEntity deviceEntity = getById(id);
        if (deviceEntity == null) {
            throw new GlobalException("地图不存在");
        }
        return deviceEntity;
    }

    @Override
    public int deleteById(Serializable id) {
        return 0;
    }

    @Override
    public int delete(Wrapper<DataRoomDeviceEntity> wrapper) {
        return 0;
    }
}
